﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SiteFox.Business.Manager
{
    public static class BlogPost
    {
        public static IList<Model.BlogPost> GetByBlog(int _ID)
        {
            var _Entities = new Model.Entities(Data.WebConfig.GetConnectionString());
            var _Query = from c in _Entities.BlogPost where c.ID_Blog == _ID orderby c.Created descending select c;
            return _Query.ToList();
        }
        public static IList<Model.BlogPost> GetByBlogCategory(int _ID)
        {
            var _Entities = new Model.Entities(Data.WebConfig.GetConnectionString());
            var _Query = from c in _Entities.BlogPost where c.ID_BlogCategory == _ID orderby c.Created descending select c;
            return _Query.ToList();
        }
        public static IList<Model.BlogPost> GetByArchive(DateTime _Date)
        {
            var _Entities = new Model.Entities(Data.WebConfig.GetConnectionString());
            var _Query = from c in _Entities.BlogPost where (c.Created.Value.Year == _Date.Year && c.Created.Value.Month == _Date.Month) orderby c.Created descending select c;
            return _Query.ToList();
        }
        public static Model.BlogPost GetByID(int _ID)
        {
            var _Entities = new Model.Entities(Data.WebConfig.GetConnectionString());
            var _Query = from c in _Entities.BlogPost where c.ID == _ID select c;
            if (_Query.Count() > 0)
            {
                return _Query.Single();
            }
            else
            {
                return null;
            }
        }
        public static void Create(Model.BlogPost _BlogPost)
        {
            var _Entities = new Model.Entities(Data.WebConfig.GetConnectionString());
            _Entities.AddToBlogPost(_BlogPost);
            _Entities.SaveChanges();
        }
        public static void Edit(Model.BlogPost _BlogPost)
        {
            var _Entities = new Model.Entities(Data.WebConfig.GetConnectionString());
            var _Query = from c in _Entities.BlogPost where c.ID == _BlogPost.ID select c;
            var __BlogPost = _Query.Single();
            __BlogPost.Created = _BlogPost.Created;
            __BlogPost.CreatedBy = _BlogPost.CreatedBy;
            __BlogPost.Description = _BlogPost.Description;
            __BlogPost.ID_Blog = _BlogPost.ID_Blog;
            __BlogPost.ID_BlogCategory = _BlogPost.ID_BlogCategory;
            __BlogPost.Modified = _BlogPost.Modified;
            __BlogPost.ModifiedBy = _BlogPost.ModifiedBy;
            __BlogPost.Text = _BlogPost.Text;
            __BlogPost.Title = _BlogPost.Title;
            _Entities.SaveChanges();
        }
        public static void Delete(int _ID)
        {
            var _Entities = new Model.Entities(Data.WebConfig.GetConnectionString());
            var _Query = from c in _Entities.BlogPost where c.ID == _ID select c;
            var _BlogPost = _Query.Single();
            _Entities.BlogPost.DeleteObject(_BlogPost);
            _Entities.SaveChanges();
        }
    }
}
